﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login
{
    private Dbcom cm;
    public Login(Dbcom CM)
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        cm = CM;
    }

    /// <summary>
    /// 获取当前用户信息
    /// </summary>
    /// <returns></returns>
    public Class_Account GetLoginCurrentAccout(string account,string password)
    {

        try
        {           
            string sql = "select a.account_id,a.account_name from t_account a where upper(a.account_name)='" +
                         account.ToUpper() + "' and a.password='" + Encrypt.EncryptStr(password) + "'";
            DataSet ds = Dbcom.GetDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Class_Account currentAccount = new Class_Account();                

                currentAccount.Account_id = ds.Tables[0].Rows[0]["account_id"].ToString();
                currentAccount.Account_name = ds.Tables[0].Rows[0]["account_name"].ToString();


                //获取所有的角色权限
                IniAccountRoleRanges(ref currentAccount);

                string sqluser= "select a1.user_id,a1.user_num,a1.user_name,dc.name as u_tech_post_name,da.name as u_position_name,"+
                                "a1.section_id,s1.section_name,a1.sickarea_id from t_userinfo a1 "+
                                "inner join t_account_user b1 on a1.user_id = b1.user_id "+
                                "inner join t_sectioninfo s1 on a1.section_id = s1.sid "+
                                "inner join t_data_code dc on dc.id = a1.u_tech_post "+
                                "inner join t_data_code da on da.id = a1.u_position  where b1.account_id="+ currentAccount.Account_id + "";



                //登录成功获取相关的帐号-用户信息
                DataSet dsuser = Dbcom.GetDataSet(sqluser);

                if (dsuser.Tables[0].Rows.Count > 0)
                {
                    //有用户信息
                    currentAccount.UserInfo = new Class_User();
                    currentAccount.UserInfo.User_id = dsuser.Tables[0].Rows[0]["user_id"].ToString();   //主键
                    currentAccount.UserInfo.User_num = dsuser.Tables[0].Rows[0]["user_num"].ToString(); //工号
                    currentAccount.UserInfo.User_name = dsuser.Tables[0].Rows[0]["user_name"].ToString();        //姓名
                    currentAccount.UserInfo.U_position_name = dsuser.Tables[0].Rows[0]["u_position_name"].ToString();  //职务
                    currentAccount.UserInfo.U_tech_post_name = dsuser.Tables[0].Rows[0]["u_tech_post_name"].ToString(); //职称
                    currentAccount.UserInfo.Section_id = dsuser.Tables[0].Rows[0]["section_id"].ToString();       //科室代码
                    currentAccount.UserInfo.Sickarea_id = dsuser.Tables[0].Rows[0]["sickarea_id"].ToString();      //病区代码
                    currentAccount.UserInfo.Section = dsuser.Tables[0].Rows[0]["section_name"].ToString();          //科室名称                           
                    return currentAccount;
                }
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

    /// <summary>
    /// 当有时间约束的时候进行限制
    /// </summary>
    /// <param name="roleid"></param>
    /// <returns></returns>
    public bool IsIntimeSpan(string roleid)
    {
        bool flag = false;       
        DataSet ds_time_span = Dbcom.GetDataSet("select * from T_USE_TIME_SPAN h where h.relation_id=" + roleid + "");
        if (ds_time_span.Tables[0].Rows.Count > 0)
        {
            //判断是否有时间有效期的设定
            DateTime starttime = Convert.ToDateTime(ds_time_span.Tables[0].Rows[0]["begin_time"].ToString());
            DateTime endttime = Convert.ToDateTime(ds_time_span.Tables[0].Rows[0]["end_time"].ToString());
            DateTime Systime = Dbcom.GetSystemTime();
            string c1 = ds_time_span.Tables[0].Rows[0]["begin_logic"].ToString();
            string c2 = ds_time_span.Tables[0].Rows[0]["end_logic"].ToString();
            if (starttime > endttime)
            {
                endttime = endttime.AddDays(1);
                //跨天    
                if (c1 == "1")
                {
                    if (Systime >= starttime)
                    {
                        if (c2 == "1")
                        {
                            if (Systime <= endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                        else
                        {
                            if (Systime < endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                    }
                    else
                    {
                        flag= false;
                    }
                }
                else
                {
                    if (Systime > starttime)
                    {
                        if (c2 == "1")
                        {
                            if (Systime <= endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                        else
                        {
                            if (Systime < endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                    }
                    else
                    {
                        flag= false;
                    }
                }
            }
            else
            {
                //不跨天
                //跨天    
                if (c1 == "1")
                {
                    if (Systime >= starttime)
                    {
                        if (c2 == "1")
                        {
                            if (Systime <= endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                        else
                        {
                            if (Systime < endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                    }
                    else
                    {
                        flag= false;
                    }
                }
                else
                {
                    if (Systime > starttime)
                    {
                        if (c2 == "1")
                        {
                            if (Systime <= endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                        else
                        {
                            if (Systime < endttime)
                            {
                                flag= true;
                            }
                            else
                            {
                                flag= false;
                            }
                        }
                    }
                    else
                    {
                        flag= false;
                    }
                }
            }
        }
        else
        {
            flag= true;
        }

        return flag;
    }

    public void GetCurrentRolePermissions(ref Class_Role CurrentSelectRole)
    {
        /*
         * 获取当前角色所对应的权限
         */
        DataSet dsPerssions = Dbcom.GetDataSet("select * from T_PERMISSION where PERM_CODE in (select PERM_CODE from T_ROLE_PERMISSION where ROLE_ID=" +CurrentSelectRole.Role_id + ")");
        CurrentSelectRole.Permissions = new Class_Permission[dsPerssions.Tables[0].Rows.Count];
        for (int i = 0; i < dsPerssions.Tables[0].Rows.Count; i++)
        {
            CurrentSelectRole.Permissions[i] = new Class_Permission();
            CurrentSelectRole.Permissions[i].Id = dsPerssions.Tables[0].Rows[i]["ID"].ToString();
            CurrentSelectRole.Permissions[i].Perm_code = dsPerssions.Tables[0].Rows[i]["PERM_CODE"].ToString();
            CurrentSelectRole.Permissions[i].Perm_name = dsPerssions.Tables[0].Rows[i]["PERM_NAME"].ToString();
            CurrentSelectRole.Permissions[i].Perm_kind = dsPerssions.Tables[0].Rows[i]["PERM_KIND"].ToString();
            CurrentSelectRole.Permissions[i].Num = dsPerssions.Tables[0].Rows[i]["NUM"].ToString();
        }
    }

    /// <summary>
    /// 获取菜单项
    /// </summary>
    /// <returns></returns>
    public Class_Permission[] GetPermissions()
    {
        try
        {
            Class_Table[] tabsqls = new Class_Table[2];

            tabsqls[0] = new Class_Table();
            tabsqls[0].Sql = "select * from t_permission where PERM_KIND='1' order by num asc";
            tabsqls[0].Tablename = "permssion";

            tabsqls[1] = new Class_Table();
            tabsqls[1].Sql = "select PERM_CODE,FUNCTION,VERSION,DLLNAME from t_permission_fuctions";//,FUNCTIONIMAGE 
            tabsqls[1].Tablename = "permission_fuctions";

            DataSet ds = Dbcom.GetDataSets(tabsqls);
            Class_Permission[] MenuPermissions = new Class_Permission[ds.Tables[0].Rows.Count];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //菜单项
                MenuPermissions[i] = new Class_Permission();
                MenuPermissions[i].Id = ds.Tables["permssion"].Rows[i]["id"].ToString();
                MenuPermissions[i].Perm_code = ds.Tables["permssion"].Rows[i]["PERM_CODE"].ToString();
                MenuPermissions[i].Perm_name = ds.Tables["permssion"].Rows[i]["PERM_NAME"].ToString();
                MenuPermissions[i].Perm_kind = ds.Tables["permssion"].Rows[i]["PERM_KIND"].ToString();
                MenuPermissions[i].Num = ds.Tables["permssion"].Rows[i]["NUM"].ToString();

                //菜单项详细信息
                MenuPermissions[i].Permission_Info = new Class_Permission_Info();

                DataRow[] dsinforows = ds.Tables["permission_fuctions"].Select("PERM_CODE='" + MenuPermissions[i].Perm_code + "'");
                if (dsinforows != null)
                {
                    if (dsinforows.Length > 0)
                    {
                        MenuPermissions[i].Permission_Info.Perm_code = dsinforows[0]["PERM_CODE"].ToString();
                        MenuPermissions[i].Permission_Info.Function = dsinforows[0]["FUNCTION"].ToString();
                        MenuPermissions[i].Permission_Info.Version = dsinforows[0]["VERSION"].ToString();
                        MenuPermissions[i].Permission_Info.DllName = dsinforows[0]["DLLNAME"].ToString();
                        //MenuPermissions[i].Permission_Info.Dll = (byte[])dsinfo.Tables[0].Rows[0]["PERM_DLL"];
                        //MenuPermissions[i].Permission_Info.FunctionImage = (byte[])dsinforows[0]["FUNCTIONIMAGE"];
                        //MenuPermissions[i].Permission_Info.Ismainfrom = dsinforows[0]["ismainfrom"].ToString();
                    }
                }
            }
            return MenuPermissions;
        }
        catch
        { return null; }
    }


    /// <summary>
    /// 获取权限集合多分院
    /// </summary>
    private Class_Account IniAccountRoleRanges(ref Class_Account UserAccount)
    {
        try
        {                       
            Class_Table[] tabSqls = new Class_Table[2];
            DataSet ds;
            ArrayList Roles = new ArrayList();

            /*
             * 当没有获得多分院服务器列表时
             */
            tabSqls[0] = new Class_Table();
            tabSqls[0].Sql = "select * from T_ROLE where ROLE_ID in (select ROLE_ID from T_ACC_ROLE "+
                             "where ACCOUNT_ID=(Select ACCOUNT_ID from T_ACCOUNT where upper(ACCOUNT_NAME)='" + 
                             UserAccount.Account_name.ToUpper() + "')) and enable_flag='Y'";
            tabSqls[0].Tablename = "role";

            tabSqls[1] = new Class_Table();
            string Sql_range = "select a.id,a.acc_role_id,a.section_id,a.sickarea_id,a.isbelongto,c.section_name,"+
                             "d.sick_area_name,b.role_id from T_ACC_ROLE_RANGE a left join T_ACC_ROLE b on "+
                             "a.acc_role_id=b.id left join T_SECTIONINFO c on a.section_id=c.sid left join "+
                             "T_SICKAREAINFO d on a.sickarea_id=d.said ";
            tabSqls[1].Sql = Sql_range + " where b.account_id in (select ACCOUNT_ID from T_ACCOUNT "+
                             "where upper(ACCOUNT_NAME)='" + 
                              UserAccount.Account_name.ToUpper() + "')";
            tabSqls[1].Tablename = "ranges";
            ds = Dbcom.GetDataSets(tabSqls);

            if (ds.Tables["role"] != null)
            {
                Class_Role temprole;
                for (int i = 0; i < ds.Tables["role"].Rows.Count; i++)
                {
                    temprole = new Class_Role();
                    temprole.Role_id = ds.Tables["role"].Rows[i]["ROLE_ID"].ToString();
                    temprole.Role_name = ds.Tables["role"].Rows[i]["ROLE_NAME"].ToString();
                    temprole.Enable = ds.Tables["role"].Rows[i]["ENABLE_FLAG"].ToString();
                    temprole.Section_Id = ds.Tables["role"].Rows[i]["SECTION_ID"].ToString();
                    temprole.Sickarea_Id = ds.Tables["role"].Rows[i]["SICKAREA_ID"].ToString();
                    temprole.Role_type = ds.Tables["role"].Rows[i]["ROLE_TYPE"].ToString();

                    /*
                     * 获取每个权限所对应的适用范围      
                     */
                    DataRow[] rows = ds.Tables["ranges"].Select("role_id=" + temprole.Role_id + "");
                    temprole.Rnages = new Class_Rnage[rows.Length];
                    for (int j1 = 0; j1 < rows.Length; j1++)
                    {
                        temprole.Rnages[j1] = new Class_Rnage();
                        temprole.Rnages[j1].Id = rows[j1]["id"].ToString();
                        temprole.Rnages[j1].Section_id = rows[j1]["section_id"].ToString();
                        temprole.Rnages[j1].Sickarea_id = rows[j1]["sickarea_id"].ToString();
                        temprole.Rnages[j1].Acc_role_id = rows[j1]["acc_role_id"].ToString();
                        temprole.Rnages[j1].Isbelonge = rows[j1]["isbelongto"].ToString();
                        //temprole.Rnages[j1].Shid = rows[j1]["shid"].ToString();
                        //0科室 1病区
                        if (temprole.Rnages[j1].Isbelonge == "0")
                        {
                            string HospitalName = Dbcom.ReadSqlVal("select a.sub_hospital_name from t_sub_hospitalinfo a inner join T_SECTIONINFO b on a.shid=b.shid where b.sid=" + rows[j1]["section_id"].ToString() + "", 0, "sub_hospital_name");
                            temprole.Rnages[j1].Rnagename = HospitalName + "--" + rows[j1]["section_name"].ToString();
                        }
                        else
                        {
                            string HospitalName = Dbcom.ReadSqlVal("select a.sub_hospital_name from t_sub_hospitalinfo a inner join T_SICKAREAINFO b on a.shid=b.shid where b.said=" + rows[j1]["sickarea_id"].ToString() + "", 0, "sub_hospital_name");
                            temprole.Rnages[j1].Rnagename = HospitalName + "--" + rows[j1]["sick_area_name"].ToString();
                        }
                    }
                    Roles.Add(temprole);
                }

                UserAccount.Roles = new Class_Role[Roles.Count];
                for (int i = 0; i < Roles.Count; i++)
                {
                    UserAccount.Roles[i] = Roles[i] as Class_Role;
                }
            }
            return UserAccount;
        }
        catch
        {
            return null;
        }
    }




}